package com.deepbreath.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.deepbreath.common.Ctype;
import com.deepbreath.entity.CitySite;
import com.deepbreath.entity.Position;
import com.deepbreath.mapper.CitySiteMapper;
import com.deepbreath.mapper.PositionMapper;
import com.deepbreath.service.IPositionService;
import com.deepbreath.service.support.Dimension;
import com.deepbreath.spider.pm25in.PositionProcessor;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

	@Resource
	private CitySiteMapper citySiteMapper;

	@Override
	public boolean doPositionSipder() {
		List<CitySite> citySites = citySiteMapper.queryCitySites(Ctype.CITY.toString());
		if (citySites == null || citySites.isEmpty()) {
			return false;
		}

		System.out.println("开始执行任务,任务数量:" + citySites.size());

		PositionProcessor processor = new PositionProcessor();
		processor.initialize(citySites);
		processor.start();

		Collection<Position> collection = processor.getDATA_MAP();
		System.out.println("任务执行完成,结果数量:" + collection.size());

		// 插入完成后插入数据库
		boolean flag = false;
		if (!collection.isEmpty()) {
			List<Position> positions = new ArrayList<Position>();
			positions.addAll(collection);
			flag = this.insertBatch(positions);
		}

		processor.shutdown();
		return flag;
	}

	@Override
	public List<Position> get24HourDatas(String positionName, Dimension dimension) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Position> get30DayDatas(String positionName, Dimension dimension) {
		// TODO Auto-generated method stub
		return null;
	}

}